<?php

namespace app\admin\model;

use app\helper\TxApi;
use think\Model;

class Recode extends Model
{
    public $autoWriteTimestamp='int';
    protected $createTime='created_time';
    protected $updateTime=false;
    protected $append=['admin_id'];
    protected $readonly=['admin_id'];

    protected function addAdminIdAttr(){
        return session('userInfo')['id'];
    }

    public function Admin(){
        return $this->belongsTo('Admin','admin_id','id');
    }
    public function add($param){
        $post_param=[
            'domain'=>$param['domain'],
            'subDomain'=>$param['sub'],
            'recordType'=>$param['type'],
            'recordLine'=>'默认',//妈的这个是，这个是线路类型，不能随便填
            'Action'=>'RecordCreate',
            'value'=>$param['recode'],
        ];
        $tx_api=new TxApi();
        $record=$tx_api->record_create($post_param);
        $param['recode_id']=$record['id'];
        $param['admin_id']=session('userInfo')['id'];
        $param['status']=$record['status'];
        $this->allowField(true)
            ->isUpdate(false)
            ->save($param);
    }
    protected function getUrlAttr($_,$data){
        return "{$data['sub']}.{$data['domain']}";
    }
    public function edit($param){
        $post_param=[
            'recordId'=>$this->recode_id,
            'domain'=>$param['domain'],
            'subDomain'=>$param['sub'],
            'recordType'=>$param['type'],
            'recordLine'=>'默认',//妈的这个是，这个是线路类型，不能随便填
            'Action'=>'RecordModify',
            'value'=>$param['recode'],
        ];
        $tx_api=new TxApi();
        $recode=$tx_api->recode_update($post_param);
        $param['recode_id']=$recode['id'];
        $param['status']=$recode['status'];
        $this->isUpdate(true)
            ->allowField(true)
            ->save($param);
    }
    public function remove(){
        $post_param=[
            'recordId'=>$this->recode_id,
            'domain'=>$this->domain,
            'Action'=>'RecordDelete'
        ];
        $tx_api=new TxApi();
        $tx_api->recode_remove($post_param);
        $this->delete();
    }
}
